package com.swlz.servlet.admin;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.swlz.R;
import com.swlz.dto.UserFormDTO;
import com.swlz.dto.UserSearch;
import com.swlz.entity.User;
import com.swlz.service.RoleService;
import com.swlz.service.UserService;
import com.swlz.service.impl.RoleServiceImpl;
import com.swlz.service.impl.UserServiceImpl;
import com.swlz.servlet.HttpServletSupport;
import com.swlz.commons.PasswordUtil;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.extra.servlet.ServletUtil;

/**<pre>
 * - 
 * ClassName : com.swlz.servlet.admin.UserListServlet
 * Author : swlz
 * E-Mail : 2233875735@qq.com
 * Date : 2020年7月25日 上午10:51:10
 * Version : 1.0
 * Copyright 2020 swlz.top Inc. All rights reserved. 
 * Warning: this content is only for internal circulation of the company. 
 * It is forbidden to divulge it or use it for other commercial purposes.
 * </pre>
 **/
@WebServlet("/admin/user.let")
public class UserServlet extends HttpServletSupport {

	private UserService userService = new UserServiceImpl();
	private RoleService roleService = new RoleServiceImpl();

	public Object page(UserSearch search) throws Exception {	
		return userService.page(search);
	}
	
	public Object updateStatus(User user) throws Exception {
		User user1 = new User();
		user1.setUserId(user.getUserId());
		user1.setUserStatus(user.getUserStatus());
		userService.update(user1);
		return R.ok();
	}
	
	public Object delete(User user) throws Exception {
		log.debug(user.toString());
		userService.delete(user.getUserId());
		return R.ok();
	}
	
	public Object listRoles() throws Exception {
		return roleService.list();
	}
	
	public String findUser(User user,HttpServletRequest request) throws Exception {
		request.setAttribute("user", userService.get(user.getUserId()));
		request.setAttribute("role", roleService.get(userService.get(user.getUserId()).getRoleId()));
		log.debug(user.toString());
		return "forward:/admin/user/findUser.jsp";
	}
	
	public String updateUserForm(User user,HttpServletRequest request) throws Exception {
		log.debug(user.toString());
		request.setAttribute("user", userService.get(user.getUserId()));
		request.setAttribute("role", roleService.get(userService.get(user.getUserId()).getRoleId()));
		
		return "forward:/admin/user/update.jsp";
	}
	
	public String updateUser(User user,HttpServletRequest request) throws Exception {
		log.debug(user.toString());
		
		userService.update(user);
		return "forward:/admin/user/page.jsp";
	}
	
	public Object updatePassw(User user) throws Exception {
		
		user.setUserPassword("123456");
		
		String userSalt = UUID.randomUUID().toString().replace("-", "");
		String userPassword = PasswordUtil.encode(user.getUserPassword(), userSalt);

		User user1 = new User();

		BeanUtil.copyProperties(user, user1);
		user1.setUserPassword(userPassword);
		user1.setUserSalt(userSalt);
		
		log.debug(user.toString());
		userService.update(user1);
		return R.ok();
	}
	
	public String addUser(UserFormDTO user,HttpServletRequest request) throws Exception {
		log.debug(user.toString());
		
		userService.add(user);
		return "forward:/admin/user/page.jsp";
	}
}
